package com.itheima.health.config;

import com.itheima.health.interceptor.SessionInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 配置运行所有的请求跨域
 */
@Configuration
@Slf4j
public class WebConfiguration implements WebMvcConfigurer {

    // 无需拦截的路径
    private String[] unLoginAccessUrlList = new String[]{
            "/user/login",
            "/user/logout",
            "/common/**",
            "/mobile/**",// 移动端的无需登录都可以访问接口
    };

    @Autowired
    private SessionInterceptor sessionInterceptor;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")// 针对所有的请求
            .allowedOrigins("*")    // 允许来自所有的域
            .allowedMethods("*")    // 允许所有的HTTP Method
            .allowCredentials(true);// 允许携带cookie
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        log.info("注册拦截器");
        registry.addInterceptor(sessionInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(unLoginAccessUrlList);
    }
}